When you play a game, the program should find data for loading monsters, weapons, maps, etc. Program should search the value and load it as soon as possible.
Therefore, number of search methods is invented and used in the programs.
We are going to explain about Sequential, binary, bubble, and merge methods.
1. Sequential (also known as a linear search) - searches every single slot and works on any unsorted data. It operates by checking every element of a list, starting from slot 0, one at a time in sequence until a match is found.
Cons: takes too long to search.
Pros: impossible to miss something, easy to make, and works on unsorted data.
ex
3 1 4 1 5 9 2 6 5 4
search (7) = false or -1
search (9) = true or 5
2. Binary - searches any data by spliting the sorted data in half. Used for for finding a particular value in a sorted list.
It makes progressively better guesses, and closes in on the sought value by selecting the median element in a list, comparing its value to the target value, and determining if the selected value is greater than, less than, or equal to the target value. A guess that turns out to be too high becomes the new top of the list, and a guess that is too low becomes the new bottom of the list. Pursuing this strategy iteratively, it narrows the search by a factor of two each time, and finds the target value.
Cons: only works on sorted data, not as easy to write than sequential.
Pros: fast, efficient.
To actually figure this out by tracing it and not actually putting it into blue J, you need some math.
Its really simple:
item#s1 2 3 4 5 6 7 8 9
slot#'s 0 1 2 3 4 5 6 7 8
This array is sorted, meaning it is all in order, to do a binary search the items must be sorted or it WILL NOT work.
Say that we want to search for 6.
We will have to start at the beggining and the end, so;
you add the first slot (0) and the last slot (8) and then divide them by 2 to find out your middle slot.
(0+8)/2= 4. So now we look at slot 4, this slot contains 5, this means that everything to the left of slot 4 including slot 4 is no longer needed.
Now, slots 5 through 8 are left, so we do the math again.
(5+8)/2 = 6. now we look at slot 6, which contains 7, remember we are searching for the number 6.
Everything to the right and including slot 6 is now out of the question.
Now, slot 5 is the only one left, this obviously has what we are looking for, but the math is as shown.
(5+5)/2= 5. Slot 5 contains the number 6 that we were looking for, and now the binary search is complete.
Writing a Sequential Search
In sequential searches, this line of code will always be used:
for(int i = 0; i < myArray.length(); i++)//myArray is replaced by whatever the name of the array is
For sequential searches involving ArrayLists, it is slightly different;
for(int i = 0; i < myArray.size(); i++)//myArray is replaced by whatever the name of the array is/*size is used in place of length because the length of an arraylist
can change */
Next you need to find if the current slot contains the value you are looking for:
if( current.equals(valueYouAreLookingFor))return i;//This returns the slot the number was found in
The finished code looks like this:
public static seqSearch(int value)
{
for(int i = 0; i < myArray.size(); i++)
{
if(currentSlot.contains(value))
return i;
}
return -1;
//This returns a dummy value if the value is not found
}
3. Bubble- this is another type of sort. It starts by comparing the 0th and 1st slots of your list of data and switching the larger element into the 1st slot. Then it moves on comparing the 1st and 2nd slots and then placing the larger element into the 2nd slot. It then continues the process until the last element in the list is the largest element. Then it restarts the process, ignoring the last slot, attempting to find the second largest element and place it in the second to last slot. Then it repeats the entire process until the list is from smallest to largest.
public class ShortBus extends FrameWindowController
{
private Mii [] myMiis;
private final int NUMMIIS = 10;
private Random gen;
private int current=0;
public void begin()
{
gen = new Random();
myMiis = new Mii[NUMMIIS];
for(int i = 0; i<myMiis.length; i++)
{
int width = gen.nextInt(50)+75;
int height = gen.nextInt(50)+175;
myMiis[i] = new Mii(100*i+25, 300-height, width, height, canvas);
}
printHeights();
} public void printHeights()
{
for(int i =0; i<myMiis.length; i++)
System.out.print( myMiis[i].getHeight() + ", ");
System.out.println();
}
public void onMousePress(Location point)
{
if( current>= myMiis.length)
{
return;
}
int shortestIndex=current;
for(int i=current; i< myMiis.length; i++)
{
if( myMiis[shortestIndex].getHeight() > myMiis[i].getHeight() )
shortestIndex = i;
}
int currentX=myMiis[current].getX();
int shortestX=myMiis[shortestIndex].getX();
myMiis[current].move(shortestX-currentX, 0);
myMiis[shortestIndex].move(currentX-shortestX, 0);
Mii tempura= myMiis[current];
myMiis[current]= myMiis[shortestIndex];
myMiis[shortestIndex]= tempura;
current++;//increment your slot to the next one
printHeights();
}
}
*
4. Merge- this is yet another type of sort. It starts by dividing the list into halves, then those new lists into halves, and repeating the process until the mini-lists all have a length of two. The next step is to compare the values and place the smaller value in the front (for each mini-list). Once all of these lists have been sorted, the next step is to compare the first elements for a pair of mini-lists and sorting them in that way until the four elements are in order. Then this process is continued for the sequential levels until it is one large sorted list.
Therefore, number of search methods is invented and used in the programs.
We are going to explain about Sequential, binary, bubble, and merge methods.
1. Sequential (also known as a linear search) - searches every single slot and works on any unsorted data. It operates by checking every element of a list, starting from slot 0, one at a time in sequence until a match is found.
Cons: takes too long to search.
Pros: impossible to miss something, easy to make, and works on unsorted data.
2. Binary - searches any data by spliting the sorted data in half. Used for for finding a particular value in a sorted list.
It makes progressively better guesses, and closes in on the sought value by selecting the median element in a list, comparing its value to the target value, and determining if the selected value is greater than, less than, or equal to the target value. A guess that turns out to be too high becomes the new top of the list, and a guess that is too low becomes the new bottom of the list. Pursuing this strategy iteratively, it narrows the search by a factor of two each time, and finds the target value.
Cons: only works on sorted data, not as easy to write than sequential.
Pros: fast, efficient.
To actually figure this out by tracing it and not actually putting it into blue J, you need some math.
Its really simple:
item#s1 2 3 4 5 6 7 8 9
slot#'s 0 1 2 3 4 5 6 7 8
This array is sorted, meaning it is all in order, to do a binary search the items must be sorted or it WILL NOT work.
Say that we want to search for 6.
We will have to start at the beggining and the end, so;
you add the first slot (0) and the last slot (8) and then divide them by 2 to find out your middle slot.
(0+8)/2= 4. So now we look at slot 4, this slot contains 5, this means that everything to the left of slot 4 including slot 4 is no longer needed.
Now, slots 5 through 8 are left, so we do the math again.
(5+8)/2 = 6. now we look at slot 6, which contains 7, remember we are searching for the number 6.
Everything to the right and including slot 6 is now out of the question.
Now, slot 5 is the only one left, this obviously has what we are looking for, but the math is as shown.
(5+5)/2= 5. Slot 5 contains the number 6 that we were looking for, and now the binary search is complete.
Writing a Sequential Search
In sequential searches, this line of code will always be used:
For sequential searches involving ArrayLists, it is slightly different;
Next you need to find if the current slot contains the value you are looking for:
The finished code looks like this:
public static seqSearch(int value) { for(int i = 0; i < myArray.size(); i++) { if(currentSlot.contains(value)) return i; } return -1; //This returns a dummy value if the value is not found }3. Bubble- this is another type of sort. It starts by comparing the 0th and 1st slots of your list of data and switching the larger element into the 1st slot. Then it moves on comparing the 1st and 2nd slots and then placing the larger element into the 2nd slot. It then continues the process until the last element in the list is the largest element. Then it restarts the process, ignoring the last slot, attempting to find the second largest element and place it in the second to last slot. Then it repeats the entire process until the list is from smallest to largest.Here is an example of a bubble sort:
import objectdraw.*;
import java.awt.*;
import java.util.*;
public class ShortBus extends FrameWindowController
{
private Mii [] myMiis;
private final int NUMMIIS = 10;
private Random gen;
private int current=0;
public void begin()
{
gen = new Random();
myMiis = new Mii[NUMMIIS];
for(int i = 0; i<myMiis.length; i++)
{
int width = gen.nextInt(50)+75;
int height = gen.nextInt(50)+175;
myMiis[i] = new Mii(100*i+25, 300-height, width, height, canvas);
}
printHeights();
}
public void printHeights()
{
for(int i =0; i<myMiis.length; i++)
System.out.print( myMiis[i].getHeight() + ", ");
System.out.println();
}
public void onMousePress(Location point)
{
if( current>= myMiis.length)
{
return;
}
int shortestIndex=current;
for(int i=current; i< myMiis.length; i++)
{
if( myMiis[shortestIndex].getHeight() > myMiis[i].getHeight() )
shortestIndex = i;
}
int currentX=myMiis[current].getX();
int shortestX=myMiis[shortestIndex].getX();
myMiis[current].move(shortestX-currentX, 0);
myMiis[shortestIndex].move(currentX-shortestX, 0);
Mii tempura= myMiis[current];
myMiis[current]= myMiis[shortestIndex];
myMiis[shortestIndex]= tempura;
current++;//increment your slot to the next one
printHeights();
}
}
*
4. Merge- this is yet another type of sort. It starts by dividing the list into halves, then those new lists into halves, and repeating the process until the mini-lists all have a length of two. The next step is to compare the values and place the smaller value in the front (for each mini-list). Once all of these lists have been sorted, the next step is to compare the first elements for a pair of mini-lists and sorting them in that way until the four elements are in order. Then this process is continued for the sequential levels until it is one large sorted list.
94628374
9462 || 8374
94 | 62 || 83 | 74
49 | 26 || 38 | 47
2469 || 3478
23446789